﻿using BW.EatRabbit.API.Application.Commands.Slaughter;
using BW.EatRabbit.Domain;
using BW.EatRabbit.ErrorCode;
using BW.EatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace BW.EatRabbit.API.Application.CommandHandlers.Slaughter
{
    public class SlaughterQueryCommandHandler : IRequestHandler<SlaughterQueryCommand, APIResult<Slaughter_Information>>
    {
        private readonly IBaseRepository<Slaughter_Information> _slaughterRepository;
        public SlaughterQueryCommandHandler(IBaseRepository<Slaughter_Information> slaughterRepository)
        {

            _slaughterRepository = slaughterRepository;
        }
        public Task<APIResult<Slaughter_Information>> Handle(SlaughterQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<Slaughter_Information> result = new APIResult<Slaughter_Information>();

            var list= _slaughterRepository.GetAll().FirstOrDefault(x=>x.ProductionBatch==request.ProductionBatch);
            if(list==null)
            {
                result.Code = ResultCode.查询失败;
                result.Message = ResultCode.查询失败.ToString();

            }
            else
            {
                result.Code = ResultCode.查询成功;
                result.Message = ResultCode.查询成功.ToString();
                result.Data = list;
            }
            return Task.FromResult(result);
        }
    }
}
